﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WS.Proj.BusinessEntity;
using System.Configuration;

namespace WS.Proj.EmployeeService
{
    public class EmployeeDL
    {
        private const string _ConStr ="EmpCon";

        private SqlConnection _sqlcon = new SqlConnection(ConfigurationManager.AppSettings[_ConStr]);

        public List<Employee> getEmployeeList(Employee objEmployee)
        {
            List<Employee> objEmployeeList = new List<Employee>();
            SqlCommand objSqlcmd = new SqlCommand("GetEmpList", _sqlcon);
            objSqlcmd.CommandType = CommandType.StoredProcedure;

            _sqlcon.Open();
            SqlDataReader objsdr = objSqlcmd.ExecuteReader();
            while (objsdr.Read())
            {
                objEmployeeList.Add(setEmployee(objsdr));
            }
            _sqlcon.Close();
            return objEmployeeList;
        }

        private Employee setEmployee(IDataReader objDR)
        {
            Employee objEmployee = new Employee();
            if (objDR != null)
            {
                if (objDR["EmpID"] != null)
                {
                    objEmployee.EmpID = Convert.ToInt32(objDR["EmpID"]);
                }
                if (objDR["EmpNo"] != null)
                {
                    objEmployee.EmpNo = Convert.ToInt32(objDR["EmpNo"]);
                }
                if (objDR["EmpName"] != null)
                {
                    objEmployee.EmpName = Convert.ToString(objDR["EmpName"]);
                }
                if (objDR["DOB"] != null)
                {
                    objEmployee.DOB = Convert.ToDateTime(objDR["DOB"]);
                }
                if (objDR["CreatedBy"] != null && !string.IsNullOrEmpty(objDR["CreatedBy"].ToString()))
                {
                    objEmployee.CreatedBy = Convert.ToString(objDR["CreatedBy"]);
                }
                if (objDR["CreatedOn"] != null && !string.IsNullOrEmpty(objDR["CreatedOn"].ToString()))
                {
                    objEmployee.CreatedOn = Convert.ToDateTime(objDR["CreatedOn"]);
                }
                if (objDR["EditedBy"] != null && !string.IsNullOrEmpty(objDR["EditedBy"].ToString()))
                {
                    objEmployee.EditedBy = Convert.ToString(objDR["EditedBy"]);
                }
                if (objDR["EditedOn"] != null && !string.IsNullOrEmpty(objDR["EditedOn"].ToString()))
                {
                    objEmployee.EditedOn = Convert.ToDateTime(objDR["EditedOn"]);
                }
            }
            return objEmployee;
        }
    }
}
